package concurrent;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by pc on 2015/4/27.
 */

class Task implements Runnable {
    private final int id;

    public Task(final int taskID) {
        id = taskID;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("I am worker: " + id + " at thread " + Thread.currentThread().getName());
    }
}

public class ThreadPoolExecutorDemo {
    public static void main(String[] args) {
        BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 6, 1, TimeUnit.DAYS, queue);
        for (int i = 0; i < 20; i++) {
            executor.execute(new Task(i + 1));
//            executor.execute(new Runnable() {
//                public void run() {
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    System.out.println(String.format("thread　%d, %s finished", this.hashCode(), Thread.currentThread().getName()));
//                }
//            });
        }
        executor.shutdown();
    }
}
